package com.ssm.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;

import com.sun.prism.impl.Disposer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.pojo.*;
import com.ssm.service.GuanService;

@Controller
@RequestMapping("/guan")
public class GuanController {
     @Autowired
    private GuanService guanService;
    @RequestMapping("/index")
    public String toIndex() {
        return "/guan/index";
    }

    @RequestMapping("/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("yong");
        return "redirect:/index";
    }

    @RequestMapping("/show")
    //这里也有long转String Long.toString(record.getTypeid()) != null
    public String toshow(Model model, Record record) {
        if (Long.toString(record.getTypeid()) != null) {
            model.addAttribute("typeid", record.getTypeid());
        }
        //要留意这个record.getName()
        if (!"".equals(record.getName()) || record.getName().length() > 0) {
            model.addAttribute("yongname", record.getName());
        }
        Page page = guanService.getRecord(record);
        model.addAttribute("page", page);
        model.addAttribute("currentPage", record.getCurrentPage());
        model.addAttribute("totalPage", page.getTotalPage());
        return "/guan/show";
    }

    @RequestMapping("/allow")
    public String allowBorrows(Integer id) {
        // 审批，并增加未归还书籍
        guanService.updateRecord(id);
        guanService.insertBackBooks(id);
        //书籍数目减1
        int booksId =guanService.selectBooksId(id);
        return "redirect:/guan/show";
    }

    @RequestMapping("/notAllow")
    public String notAllow(long id) {
        guanService.notAllowRecord(id);
        //更新结束时间
        Date back = new Date();
        Record record = new Record();
        record.setId(id);
        record.setBack(back);
        guanService.updateRecordTime(record);
        return "redirect:/guan/show";
    }

    @RequestMapping("/backBooks")
    //这里的是record
    public String getBackBooks(Model model,Record record) {
        Page page = guanService.getBackBooks(record);
        model.addAttribute("page", page);
        model.addAttribute("currentPage", record.getCurrentPage());
        model.addAttribute("totalPage", page.getTotalPage());

        return "/guan/showBackBook";
    }

    @RequestMapping("/allowBackBook")
    public String allowBackBook(long id) {
        guanService.allowBackBook(id);
        int recordId = guanService.selectRecordId(id);
        int booksId = guanService.selectBooksId(recordId);
        // 书籍数目加1
        guanService.updateBooks(booksId);
        // 更新归还时间
        Date back = new Date();
        Record record = new Record();
        record.setId(recordId);
        record.setBack(back);
        guanService.updateRecordTime(record);
        return "redirect:/guan/backBooks";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "guan/add";
    }

    @RequestMapping("/show1")
    public String getBooks(Model model, Book book) {
        System.out.println(book);
        if (Long.toString(book.getTypeid()) != null) {
            model.addAttribute("typeid", book.getTypeid());
        }
        if (!"".equals(book.getName()) || book.getName().length() > 0) {
            model.addAttribute("name", book.getName());
        }

        Page page = guanService.getBooks(book);
        model.addAttribute("page", page);
        model.addAttribute("currentPage", book.getCurrentPage());
        model.addAttribute("totalPage", page.getTotalPage());
        return "guan/show1";
    }

    @RequestMapping("/add")
    public String addBooks(Book book) {
        guanService.addBooks(book);
        return "redirect:/guan/show1";
    }

    @RequestMapping("/selById")
    public String selBookById(Model model, Integer id) {
        Book book = guanService.getBookById(id);
        model.addAttribute("book", book);
        return "guan/update";
    }

    @RequestMapping("/update")
    public String updateBooks(Book book) {
        guanService.updateBooks(book);
        return "redirect:/guan/show1";
    }

    @RequestMapping("/delete")
    public String deleteBooks(long id) {
        guanService.deleteBooks(id);
        return "redirect:/guan/show1";
    }



    // 下面部分为Yong模块
    @RequestMapping("/showYong")
    public String showYong(Model model,Yong yong){
        List<Yong> yongs = guanService.getYong(yong);
        model.addAttribute("yongs",yongs);
        return "guan/showYong";
    }

    @RequestMapping("/toAddYong")
    public String toAddYong() {
        return "guan/addYong";
    }

    @RequestMapping("/addYong")
    public String addYong(Yong yong) {
        guanService.addYong(yong);
        return "redirect:/guan/showYong";
    }

    @RequestMapping("/selUserById")
    public String selYongById(Model model, long id) {
        Yong yong = guanService.getYongById(id);
        model.addAttribute("yong", yong);
        return "guan/updateYong";
    }

    @RequestMapping("/updateYong")
    public String updateYong(Yong yong) {
        guanService.updateYong(yong);
        return "redirect:/guan/showYong";
    }

    @RequestMapping("/deleteYong")
    public String deleteYong(long id) {
        guanService.deleteYong(id);
        return "redirect:/guan/showYong";
    }

    @RequestMapping("/checkCode")
    @ResponseBody
    public String checkCode(String name) {
        int yong = guanService.selectYong(name);
        if (yong != 0) {
            return "1";
        }
        return "0";
    }

}
